var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: '', //客户id
    phone: '', //电话
    name: '', //姓名
    plateNumber: '', //车牌号
    idNumber: '', //身份证号
    cardNumber: '', //IC卡号
    bankNumber: '', // 银行卡号
    bankCardName: '' //所属银行
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},
  //银行卡号前端校验
  onbankCardNameEvent: function(e) {
    var that = this;
    that.setData({ bankNumber: e.detail.value.replace(/\D/g, '') });
  },
  //手机号前端校验
  onPhoneEvent: function(e) {
    var that = this;
    if (e.detail.value.length == 1) {
      that.setData({ phone: e.detail.value.replace(/[^1-9]/g, '') });
    } else {
      that.setData({ phone: e.detail.value.replace(/\D/g, '') });
    }
    if (e.detail.value.length == 11) {
      wx.request({
        method: 'POST',
        url: app.preUrl + 'getUserBaseByPhone',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          phone: that.data.phone
        },
        success: function(res) {
          console.log();
          if (res.data.code == 100) {
            let data = res.data.result;
            console.log(data.id, app.globalData.userId);
            if (data.id) {
              if (data.id == app.globalData.userId) {
                wx.showModal({
                  title: '提示',
                  content: '不能添加自己为客户',
                  success(res1) {
                    if (res1.confirm) {
                      that.setData({
                        phone: '',
                        name: ''
                      });
                    } else if (res1.cancel) {
                      console.log('用户点击取消');
                      that.setData({
                        phone: '',
                        name: ''
                      });
                    }
                  }
                });
              }
              that.setData({
                userId: data.id || '', //客户id
                name: data.contacts || '', //姓名
                plateNumber: data.plateNum || '', //车牌号
                idNumber: data.cardNum || '', //身份号
                bankCardName: data.bankCardName || '', //所属银行
                bankNumber: data.bankCardNumber || '' //银行卡号
              });
            }
          }
        },
        fail: function(error) {
          that.setData({ buthiddenSubmit: false });
        }
      });
    }
  },
  bindName(e) {
    this.setData({
      name: e.detail.value.replace(/(^\s*)|(\s*$)/g, '')
    });
  },
  bindPlateNumber(e) {
    this.setData({
      plateNumber: e.detail.value.replace(/(^\s*)|(\s*$)/g, '')
    });
  },
  bindCardNumber(e) {
    this.setData({
      cardNumber: e.detail.value.replace(/(^\s*)|(\s*$)/g, '')
    });
  },
  bindIdNumber(e) {
    this.setData({
      idNumber: e.detail.value.replace(/(^\s*)|(\s*$)/g, '')
    });
  },
  bindBankNumber(e) {
    this.setData({
      bankNumber: e.detail.value.replace(/(^\s*)|(\s*$)/g, '')
    });
  },
  bindFormSubmit() {
    var that = this;
    var userId = that.data.userId;
    var phone = that.data.phone;
    var name = that.data.name;
    var plateNumber = that.data.plateNumber;
    var idNumber = that.data.idNumber;
    var cardNumber = that.data.cardNumber;
    var bankNumber = that.data.bankNumber;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!phone) {
      wx.showToast({
        title: '手机号必填',
        icon: 'none'
      });
      return false;
    } else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none'
      });
      return false;
    } else if (!name) {
      wx.showToast({
        title: '客户姓名必填',
        icon: 'none'
      });
      return false;
    } else if (
      (cardNumber.length && cardNumber.length < 6) ||
      (cardNumber.length && cardNumber.length > 18)
    ) {
      wx.showToast({ title: 'IC卡号输入错误', icon: 'none' });
      return false;
    } else if (idNumber.length && idNumber.length != 18) {
      wx.showToast({ title: '身份证长度有误', icon: 'none' });
      return false;
    } else if (
      bankNumber.length &&
      bankNumber.length != 16 &&
      bankNumber.length != 17 &&
      bankNumber.length != 18 &&
      bankNumber.length != 19
    ) {
      wx.showToast({ title: '银行卡长度有误', icon: 'none' });
      return false;
    } else {
      wx.showLoading({ title: '提交中' });
      that.setData({ buthiddenSubmit: true });
      wx.request({
        method: 'POST',
        url: app.preUrl + 'addSupplierUser',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          packerId: app.globalData.userId, //打包站ID
          userId: userId, //客户ID
          phone: phone,
          name: name,
          plateNumber: plateNumber, //车牌号
          idNumber: idNumber, //身份证号
          cardNumber: cardNumber, //ic卡号
          bankNumber: bankNumber //银行卡号
        },
        success: function(res) {
          wx.hideLoading();
          if (res.data.code == 100) {
            wx.showToast({
              title: '绑定成功',
              icon: 'success',
              duration: 1000
            });
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              });
            }, 1500);
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            });
            that.setData({ buthiddenSubmit: false });
          }
        },
        fail: function(error) {
          that.setData({ buthiddenSubmit: false });
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
// else if (!plateNumber) {
//   wx.showToast({ title: '车牌号必填', icon: 'none' })
//   return false
// } else if (!cardNumber) {
//   wx.showToast({ title: 'IC卡号必填', icon: 'none' })
//   return false
// }
